package com.lovely3x.jobservice.executors.extensions.version.executor;

import android.os.Parcel;
import android.os.Parcelable;

import com.lovely3x.common.versioncontroller.Version;
import com.lovely3x.jobservice.task.Progress;

/**
 * 版本检查进度
 * Created by lovely3x on 16-2-18.
 */
public class VersionCheckProgress implements Progress, Parcelable {

    /***
     * 不可预计
     */
    public static final int INEVITABLE = -1;
    /**
     * 任务的what，标识该进度属于什么任务
     */
    public long task = -1;
    /**
     * 该任务进度什么时候发布的
     */
    public long when;
    /**
     * 任务当前的状态
     */
    public int state = State.IDLE;
    /**
     * 执行任务需要的最大值，如果任务是不可预估进度的返回-1
     */
    public long max = INEVITABLE;
    /**
     * 当前已经达到的值，如果任务是不可预估进度的返回-1
     */
    public long current = INEVITABLE;
    /**
     * 远程的版本对象
     */
    public Version remoteVersion;
    /**
     * 当前的版本对象
     */
    public Version locVersion;

    public VersionCheckProgress() {
    }

    @Override
    public long getTask() {
        return task;
    }

    @Override
    public void setTask(long task) {
        this.task = task;
    }

    @Override
    public long getWhen() {
        return when;
    }

    @Override
    public void setWhen(long when) {
        this.when = when;
    }

    @Override
    public int getState() {
        return state;
    }

    @Override
    public void setState(int state) {
        this.state = state;
    }

    @Override
    public long getMax() {
        return max;
    }

    @Override
    public void setMax(long max) {
        this.max = max;
    }

    @Override
    public long getCurrent() {
        return current;
    }

    public Version getRemoteVersion() {
        return remoteVersion;
    }

    public void setRemoteVersion(Version remoteVersion) {
        this.remoteVersion = remoteVersion;
    }

    @Override
    public void setCurrent(long current) {
        this.current = current;
    }

    public Version getLocVersion() {
        return locVersion;
    }

    public void setLocVersion(Version locVersion) {
        this.locVersion = locVersion;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeLong(this.task);
        dest.writeLong(this.when);
        dest.writeInt(this.state);
        dest.writeLong(this.max);
        dest.writeLong(this.current);
        dest.writeParcelable(this.remoteVersion, 0);
        dest.writeParcelable(this.locVersion, 0);
    }

    protected VersionCheckProgress(Parcel in) {
        this.task = in.readLong();
        this.when = in.readLong();
        this.state = in.readInt();
        this.max = in.readLong();
        this.current = in.readLong();
        this.remoteVersion = in.readParcelable(Version.class.getClassLoader());
        this.locVersion = in.readParcelable(Version.class.getClassLoader());
    }

    public static final Creator<VersionCheckProgress> CREATOR = new Creator<VersionCheckProgress>() {
        public VersionCheckProgress createFromParcel(Parcel source) {
            return new VersionCheckProgress(source);
        }

        public VersionCheckProgress[] newArray(int size) {
            return new VersionCheckProgress[size];
        }
    };

    @Override
    public String toString() {
        return "VersionCheckProgress{" +
                "task=" + task +
                ", when=" + when +
                ", state=" + state +
                ", max=" + max +
                ", current=" + current +
                ", remoteVersion=" + remoteVersion +
                ", locVersion=" + locVersion +
                '}';
    }

    @Override
    public boolean equals(Object object) {
        if (this == object) return true;
        if (object == null || getClass() != object.getClass()) return false;

        VersionCheckProgress that = (VersionCheckProgress) object;

        return task == that.task;

    }

    @Override
    public int hashCode() {
        return (int) (task ^ (task >>> 32));
    }
}
